package com.beizhen.demo.controller;

import com.alibaba.fastjson.JSONArray;
import com.beizhen.demo.entity.Code;
import com.beizhen.demo.service.CodeService;
import com.beizhen.demo.service.SendSms;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;
/**
 * 2020年5月18日22点16分
 * 孟希
 *发送验证码
 * @return
 */
@RestController
@CrossOrigin
public class SmsControlle {
    @Resource
    private CodeService codeService;


    @Resource
    private SendSms sendSms;


    @RequestMapping("/sendSms")
    public void code(@RequestParam(value = "uPhone") String telephone) {
        //生成验证码
        String code = UUID.randomUUID().toString().substring(0, 4);
        HashMap<String, Object> map = new HashMap<>();
     /*   boolean send = sendSms.send(telephone, map);
       if (send){
           Code code2=new Code(telephone,code);
           int res=codeService.insertInfo(code2);
           System.out.println(code);

       }
*/
        System.out.println("tel"+telephone);
        Code code2 = new Code();
        code2.setCode(code);
        code2.setPhone(telephone);
        int res = codeService.insertInfo(code2);
        System.out.println(code+"-"+res);

    }
    @GetMapping("doverifi")
    @ResponseBody
    public String verificat(@RequestParam(value = "uPhone") String phone,
                            @RequestParam(value = "verification") String verification
    ){
        Map<String,String> jsonmap=new HashMap<>();
        System.out.println("tt"+phone);
        String code = codeService.getCode(phone);
        if (code.equals(verification)) {
            jsonmap.put("result","ok");
        } else {
            jsonmap.put("result","fail");
        }
        System.out.println("jj"+jsonmap);
        return JSONArray.toJSONString(jsonmap);
    }

    /**
     *手机号快捷登录
     * @param
     * @param verification
     * @param session
     * @param model
     * @return
     */

    @GetMapping("doverification")
    public String verification(@RequestParam(value = "telphone") String phone,
                               @RequestParam(value = "verification") String verification,
                               HttpSession session,
                               Model model) {
        String code = codeService.getCode(phone);
        Map map = new HashMap();
        map.put("phone",phone);

        if (code.equals(verification)) {
            /*  User userphonepd = userService.userOne(map);*/
            return "redirect:/house/showDifferentHouse";
        } else {
            model.addAttribute("message", "验证码错误");
        }
        return "login1";
    }

}
